//package com.example.project.utils;
//
//import com.google.common.util.concurrent.ThreadFactoryBuilder;
//import org.apache.commons.compress.archivers.zip.ParallelScatterZipCreator;
//import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
//import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
//import org.apache.commons.compress.parallel.InputStreamSupplier;
//import org.apache.commons.io.input.NullInputStream;
//
//import java.io.*;
//import java.util.List;
//import java.util.concurrent.*;
//import java.util.zip.Deflater;
//
///**
// * @author youlingdada youlingdada@163.com
// * @version 1.0
// * @createDate 2022/1/30 11:15
// */
//public class FileZipUtils {
//    /**
//     * 压缩文件夹
//     *
//     * @param zipOutName zip输出流
//     * @param paths 将要压缩的路径
//     * @throws IOException
//     * @throws ExecutionException
//     * @throws InterruptedException
//     */
//    public static void compressFiles(String zipOutName, List<String> paths)
//        throws IOException, ExecutionException, InterruptedException {
//        ThreadFactory factory = new ThreadFactoryBuilder().setNameFormat("compressFileList-pool-").build();
//        ExecutorService executor = new ThreadPoolExecutor(5, 10, 60, TimeUnit.SECONDS, new LinkedBlockingQueue<>(20),
//            factory, new ThreadPoolExecutor.CallerRunsPolicy());
//        ParallelScatterZipCreator parallelScatterZipCreator = new ParallelScatterZipCreator(executor);
//        OutputStream outputStream = new FileOutputStream(zipOutName);
//        ZipArchiveOutputStream zipArchiveOutputStream = new ZipArchiveOutputStream(outputStream);
//        zipArchiveOutputStream.setLevel(Deflater.BEST_SPEED);
//        zipArchiveOutputStream.setEncoding("UTF-8");
//        // String relativePath = String.valueOf(System.currentTimeMillis()); paths.length > 1 ? relativePath + "/" +
//        // temp.getName() : temp.getName()
//        for (String path : paths) {
//            File temp = new File(path);
//            compress(parallelScatterZipCreator, temp, temp.getName());
//        }
//        // 将数据写入输出流
//        parallelScatterZipCreator.writeTo(zipArchiveOutputStream);
//        // 关闭
//        zipArchiveOutputStream.close();
//        outputStream.close();
//    }
//
//    /**
//     * 自定义线程池
//     *
//     * @param zipOutName
//     * @param executorService 线程池实现对象
//     * @param paths
//     * @throws IOException
//     * @throws ExecutionException
//     * @throws InterruptedException
//     */
//    public static void compressFiles(String zipOutName, ExecutorService executorService, String... paths)
//        throws IOException, ExecutionException, InterruptedException {
//        ParallelScatterZipCreator parallelScatterZipCreator = new ParallelScatterZipCreator(executorService);
//        OutputStream outputStream = new FileOutputStream(zipOutName);
//        ZipArchiveOutputStream zipArchiveOutputStream = new ZipArchiveOutputStream(outputStream);
//        zipArchiveOutputStream.setEncoding("UTF-8");
//        for (String path : paths) {
//            File temp = new File(path);
//            compress(parallelScatterZipCreator, temp, temp.getName());
//        }
//        parallelScatterZipCreator.writeTo(zipArchiveOutputStream);
//
//        zipArchiveOutputStream.close();
//        outputStream.close();
//    }
//
//    /**
//     * 自定义线程创建工程
//     *
//     * @param zipOutName
//     * @param factory 线程创建工厂
//     * @param paths
//     * @throws IOException
//     * @throws ExecutionException
//     * @throws InterruptedException
//     */
//    public static void compressFiles(String zipOutName, ThreadFactory factory, String... paths)
//        throws IOException, ExecutionException, InterruptedException {
//        ExecutorService executor = new ThreadPoolExecutor(5, 10, 60, TimeUnit.SECONDS, new LinkedBlockingQueue<>(20),
//            factory, new ThreadPoolExecutor.CallerRunsPolicy());
//        ParallelScatterZipCreator parallelScatterZipCreator = new ParallelScatterZipCreator(executor);
//        OutputStream outputStream = new FileOutputStream(zipOutName);
//        ZipArchiveOutputStream zipArchiveOutputStream = new ZipArchiveOutputStream(outputStream);
//        zipArchiveOutputStream.setEncoding("UTF-8");
//        for (String path : paths) {
//            File temp = new File(path);
//            compress(parallelScatterZipCreator, temp, temp.getName());
//        }
//        // 将数据写入zip输出流
//        parallelScatterZipCreator.writeTo(zipArchiveOutputStream);
//        // 关闭
//        zipArchiveOutputStream.close();
//        outputStream.close();
//    }
//
//    /**
//     * 遍历压缩
//     *
//     * @param parallelScatterZipCreator 线程池压缩对象
//     * @param inputFile 将要压缩的文件路径,绝对路径
//     * @param relativePath 相对与压缩包内的路径
//     * @throws IOException
//     * @throws ExecutionException
//     * @throws InterruptedException
//     */
//    protected static void compress(ParallelScatterZipCreator parallelScatterZipCreator, File inputFile,
//        String relativePath) throws IOException, ExecutionException, InterruptedException {
//        if (inputFile == null) {
//            return;
//        }
//        if (inputFile.isDirectory()) {
//            File[] files = inputFile.listFiles();
//            if (files == null) {
//                return;
//            }
//            for (File file : files) {
//                if (file.isDirectory()) {
//                    compress(parallelScatterZipCreator, new File(inputFile.getAbsolutePath() + "/" + file.getName()),
//                        relativePath + "/" + file.getName());
//                } else {
//                    final InputStreamSupplier inputStreamSupplier = () -> {
//                        try {
//                            return new FileInputStream(file);
//                        } catch (FileNotFoundException e) {
//                            e.printStackTrace();
//                            return new NullInputStream(0);
//                        }
//                    };
//                    ZipArchiveEntry zipArchiveEntry = new ZipArchiveEntry(relativePath + "/" + file.getName());
//                    zipArchiveEntry.setMethod(ZipArchiveEntry.DEFLATED);
//                    zipArchiveEntry.setSize(file.length());
//                    parallelScatterZipCreator.addArchiveEntry(zipArchiveEntry, inputStreamSupplier);
//                }
//            }
//
//        } else {
//            final InputStreamSupplier inputStreamSupplier = () -> {
//                try {
//                    return new FileInputStream(inputFile);
//                } catch (FileNotFoundException e) {
//                    e.printStackTrace();
//                    return new NullInputStream(0);
//                }
//            };
//            ZipArchiveEntry zipArchiveEntry = new ZipArchiveEntry(relativePath + "/" + inputFile.getName());
//            zipArchiveEntry.setMethod(ZipArchiveEntry.DEFLATED);
//            zipArchiveEntry.setSize(inputFile.length());
//            parallelScatterZipCreator.addArchiveEntry(zipArchiveEntry, inputStreamSupplier);
//        }
//    }
//
//
//}
